﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using KaKaSongGame.ViewModel;

namespace KaKaSongGame
{
    public partial class MiniMap2 : UserControl
    {
      public MiniMap2()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MiniMap_Loaded);
        }


        void MiniMap_Loaded(object sender, RoutedEventArgs e)
        {
            MainPageVM = this.DataContext as MainPageViewModel;
            if (MainPageVM == null)
                throw new Exception("datacontext is not mainpageviewmodel type.");

            MainPageVM.MapWindow.PropertyChanged += MapWindow_PropertyChanged;
            MainPageVM.PropertyChanged += vm_PropertyChanged;

            InitWindowRect();
        }

        public MainPageViewModel MainPageVM{ set; get; }

        void vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "MiniMapImage")
                SetNewMap(MainPageVM.MiniMapImage);
        }

        void MapWindow_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            InitWindowRect();
        }

        void InitWindowRect()
        {
            if (mapwindowrect != null)
            {
                var rect = MainPageVM.MapWindow;
                mapwindowrect.Width = rect.Width * CastleMap.MINIMAP_SCALE;
                mapwindowrect.Height = rect.Height * CastleMap.MINIMAP_SCALE;
                Canvas.SetLeft(mapwindowrect, rect.X * CastleMap.MINIMAP_SCALE);
                Canvas.SetTop(mapwindowrect, rect.Y * CastleMap.MINIMAP_SCALE);
            }
        }

        void SetNewMap(ImageSource bitmap)
        {
            if (mapimg != null)
            {
                mapimg.Source = newmapimg.Source;
                newmapimg.Source = bitmap;
                if (mapimg.Source != null)
                    Sparkle();
            }
        }

        public void Sparkle()
        {
            var storyboard = this.Resources["Storyboard1"] as Storyboard;
            storyboard.RepeatBehavior = new RepeatBehavior(10);
            storyboard.Begin();
        }

        Point relativepos;
        bool isDrag;
        private void mapwindowrect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            relativepos = e.GetPosition(mapwindowrect);
            //this.CaptureMouse();
            isDrag = true;
            //e.Handled = true;
        }

        private void mapwindowrect_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrag)
            {
                var abspos = e.GetPosition(rectpanel);
                var rect = new Rect();
                rect.Width = this.mapwindowrect.Width;
                rect.Height = this.mapwindowrect.Height;
                rect.X = abspos.X - relativepos.X;
                rect.Y = abspos.Y - relativepos.Y;

                Dispatcher.BeginInvoke(delegate
                {
                    if (rect.Left >= 0 && rect.Right <= mappanel.ActualWidth)
                        MainPageVM.MapWindow.X = rect.X / CastleMap.MINIMAP_SCALE;
                    //Canvas.SetLeft(mapwindowrect, rect.X / CastleMap.MINIMAP_SCALE);

                    if (rect.Top >= 0 && rect.Bottom <= mappanel.ActualHeight)
                        MainPageVM.MapWindow.Y = rect.Y / CastleMap.MINIMAP_SCALE;
                    //Canvas.SetTop(mapwindowrect, rect.Y / CastleMap.MINIMAP_SCALE);
                });
            }
        }

        private void mapwindowrect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDrag = false;
        }

        private void minmax_click(object sender, RoutedEventArgs e)
        {
            if (mappanel.Visibility == Visibility.Collapsed)
                mappanel.Visibility = Visibility.Visible;
            else
                mappanel.Visibility = Visibility.Collapsed;
        }
    }
}
